jsx3.require("jsx3.gui.Block");


/**
 * A generic tabbed pane with ActiveMatrix Admin UI Visual Styles applied.
 **/
jsx3.Class.defineClass(
        "com.gix.gui.TabbedPane",
        jsx3.gui.Block,
        null,
        function (TabbedPane, TabbedPane_prototype) {

            /**
             * {String} The subject published to when one of this pane's tabs has been selected.  The published message will
             * include a field called <code>target</code> containing the tab object that was selected.
             */
            TabbedPane.TAB_SELECTED = "com.gix.gui.TabbedPane.TAB_SELECTED";

            /**
             * {com.gix.gui.Tab} the currently selecred tab
             * @private
             */
            TabbedPane_prototype.selectedTab = null;

            /**
             * instance initializer
             * @param strName {String} unique name distinguishing this object from all other JSX GUI objects in the JSX application
             * @param vntLeft {int|String} either a number (i.e, 12, 30, etc) or a number and a unit value (i.e., "25%", "36pt", etc); if a number is passed, pixels will be the assumed unit when painted to screen
             * @param vntTop {int|String} either a number (i.e, 12, 30, etc) or a number and a unit value (i.e., "25%", "36pt", etc); if a number is passed, pixels will be the assumed unit when painted to screen
             * @param vntWidth {int|String} either a number (i.e, 12, 30, etc) or a number and a unit value (i.e., "25%", "36pt", etc); if a number is passed, pixels will be the assumed unit when painted to screen
             * @param vntHeight {int|String} either a number (i.e, 12, 30, etc) or a number and a unit value (i.e., "25%", "36pt", etc); if a number is passed, pixels will be the assumed unit when painted to screen
             * @param strHTML {String} Text/HTML markup to place in the jsx3.gui.Block instance
             */
            TabbedPane_prototype.init = function(strName,vntLeft,vntTop,vntWidth,vntHeight,strHTML) {
                try {
                this.jsxsuper(strName,vntLeft,vntTop,vntWidth,vntHeight,strHTML);
                this.bgimg = "background-image: url(" + ux.admin.Main.getServer().resolveURI("jsxaddin://user!LiveCore/images/matrix/tabpane_bg.gif") + "); background-repeat: repeat; background-position: 0px 0px;";
                this.setBackgroundColor("-1");
                var firstLayout = new jsx3.gui.LayoutGrid("layout (--)", 0, 0, "100%", "100%");
                firstLayout.setRelativePosition(jsx3.gui.Block.ABSOLUTE).setOrientation(jsx3.gui.LayoutGrid.ORIENTATIONCOL).setRows("20,*").setCols("*").setBestGuess(jsx3.gui.LayoutGrid.ADAPTIVE);
                this.setChild(firstLayout, jsx3.app.Model.PERSISTEMBED, null, jsx3.app.Model.FRAGMENTNS);
                    var tabPane = new com.gix.gui.TabbedPane.TabPane("tabPane", 0, 0, "100%", "100%");
                    tabPane.setOverflow(jsx3.gui.Block.OVERFLOWHIDDEN).setCSSOverride(this.bgimg);
                    firstLayout.setChild(tabPane, jsx3.app.Model.PERSISTEMBED, null, jsx3.app.Model.FRAGMENTNS);
                    firstLayout.setChild((new jsx3.gui.Block("contentPane",0,0,"100%","100%")).setBackgroundColor("#FFFFFF").setBorder("0px solid #7b797a 1px solid #7b797a 1px solid #7b797a 1px solid #7b797a;"), jsx3.app.Model.PERSISTEMBED, null, jsx3.app.Model.FRAGMENTNS);
                tabPane.subscribe(jsx3.gui.Interactive.AFTER_RESIZE_VIEW, tabPane, tabPane.testHeight);
                } catch(ex) {
                    ux.admin.Main.logException(ex);
                }
            };

            /**
             * Adds a new tab to this tabbed pane.
             *
             * @param tabName {String} the text label for the new tab
             * @return {com.gix.gui.Tab} the newly created child tab
             */
            TabbedPane_prototype.addTab = function(tabName) {
                var tab = new com.gix.gui.Tab("tab" + tabName, 0, 0, null, tabName);
                this.setChild(tab, jsx3.app.Model.PERSISTEMBED);
                this.getTabPane().repaint();
                return tab;
            };

            TabbedPane_prototype.setChild = function(objChild, intPersist, strSourceURL, strNS) {
                var ret = null;
                if (objChild instanceof com.gix.gui.Tab) {
                    ret = this.getTabPane().setChild(objChild, intPersist, strSourceURL, strNS);
                }
                else if ((objChild instanceof jsx3.gui.Block) && !(objChild instanceof jsx3.gui.LayoutGrid)) {
                    if(!this.getTabPane() || !this.getContentPane()) {
                        ret = this.getChild(0).setChild(objChild, intPersist, strSourceURL, strNS);
                    }
                    else {
                        return false;
                    }
                }
                else {
                    if(this.getChildren().length == 0) {
                        return this.jsxsuper(objChild, intPersist, strSourceURL, strNS);
                    }
                    return false;
                }
                var me = this;
                setTimeout(function() {
                    me.repaint();
                },10);
                return ret;
            }

            /**
             * Gets the currently selected tab
             *
             * @return {com.gix.gui.Tab} the currently selected tab
             */
            TabbedPane_prototype.getSelectedTab = function() {
                return this.selectedTab;
            }

            /**
             * Upon the addition of a new tab, this function adds the tab's content pane to this container's content
             * pane.
             *
             * @private
             */
            TabbedPane_prototype.tabAdded = function(tab) {
                try {
                this.getContentPane().setChild(tab.getContentPane().setRelativePosition(jsx3.gui.Block.ABSOLUTE), jsx3.app.Model.PERSISTNONE);
                }
                catch(ex) {
                    // do nothing
                }
                if(!this.selectedTab) {
                    tab.select();
                }
                var testPane = this.getTabPane();
                setTimeout(function() {
                testPane.testHeight();
                }, 10);
            }

            TabbedPane_prototype.onSetChild = function(objChild) {
                if (objChild instanceof com.gix.gui.Tab) {
                    return true;
                }
                if(!(objChild instanceof jsx3.gui.Block)) return false;
                if ((objChild instanceof jsx3.gui.Block) && !(objChild instanceof jsx3.gui.LayoutGrid)) {
                    try {
                        if(this.getContentPane()) {
                            return false;
                        }
                        return true;
                    }
                    catch(ex) {
                        return false;
                    }
                }
                if(this.getChildren().length != 0) {
                    return false;
                }
                return this.jsxsuper(objChild);
            };

            /**
             * Gets the pane containing all of the child tabs for this container.
             *
             * @return {com.gix.gui.TabbedPane.TabPane} the pane containing all of the child tabs for this container
             */
            TabbedPane_prototype.getTabPane = function() {
                return this.getChild(0).getChild(0);
            }

            /**
             * Gets the content pane of this container.  This content pane hosts the content panes for all of the individual
             * tabs.
             *
             * @return {jsx3.gui.Block} the content pane of this container
             */
            TabbedPane_prototype.getContentPane = function() {
                return this.getChild(0).getChild(1);
            }

            /**
             * Handles the deselection of existing selected tabs if a new tab is selected, and publishes a message
             * about the selection. To be called ONLY by the child tab that has been selected!
             * @private
             */
            TabbedPane_prototype.tabSelected = function(tabObj) {
                if(this.selectedTab == tabObj) return;
                var prevTab = this.selectedTab;
                this.selectedTab = tabObj;
                if(prevTab) {
                    prevTab.getContentPane().setDisplay(jsx3.gui.Block.DISPLAYNONE, true);
                    prevTab.deselect();
                }
                tabObj.getContentPane().setDisplay(jsx3.gui.Block.DISPLAYBLOCK, true);
                this.getContentPane().repaint();
                var msg = new Object();
                msg.subject = com.gix.gui.TabbedPane.TAB_SELECTED;
                msg.target = this.selectedTab;
                this.publish(msg);
            }

            TabbedPane_prototype.onSetParent = function() {
                var ret = this.jsxsuper();
                this.bgimg = "background-image: url(" + ux.admin.Main.getServer().resolveURI("jsxaddin://user!LiveCore/images/matrix/tabpane_bg.gif") + "); background-repeat: repeat; background-position: 0px 0px;";
                this.getTabPane().setCSSOverride(this.bgimg);
//                                this.bgimg = "background-image: url(" + ux.admin.Main.getServer().resolveURI("jsxaddin://user!LiveCore/images/matrix/sectionHeaderTile.jpg") + "); background-repeat: repeat;";
//                this.getLabelPane().setCSSOverride(this.bgimg);
                if(!this.selectedTab) {
                    var children = this.getTabPane().getChildren();
                    for(var i=0; i<children.length; i++) {
                        var child = children[i];
                        this.tabAdded(child);
                    }
                }
                var tabPane = this.getTabPane();
                tabPane.subscribe(jsx3.gui.Interactive.AFTER_RESIZE_VIEW, tabPane, tabPane.testHeight);
                return ret;
            }

            /**
             * Overridden to test if the height of the tabPane needs to be resized, based on the positioning of the child
             * tabs (auto-expand/collapse the area to fit the tabs)
             * @private
             */
            TabbedPane_prototype.repaint = function() {
                this.jsxsuper();
                var testPane = this.getTabPane();
                setTimeout(function() {
                testPane.testHeight();
                }, 10);
            }
        });

/**
 * The pane containing all of the tabs for a particular com.gix.gui.TabbedPane.
 */
jsx3.Class.defineClass(
        "com.gix.gui.TabbedPane.TabPane",
        jsx3.gui.Block,
        null,
        function (TabPane, TabPane_prototype) {
            TabPane_prototype.setChild = function(child, persist, source, ns) {
                if(child instanceof com.gix.gui.Tab) {
                    var ret = this.jsxsuper(child, persist, source, ns);
                    var parent = this.getParent();
                    if(parent) {
                        parent = parent.getParent();
                        if(parent) {
                                if(parent.tabAdded) {
                                    parent.tabAdded(child);
                                }
                        }
                    }
                    return ret;
                }
                return false;
            };

            TabPane_prototype.onSetChild = function(child) {
                if (child instanceof com.gix.gui.Tab) {
                    return this.jsxsuper(child);
                }
                else {
                    return false;
                }
            };

            TabPane_prototype.testHeight = function() {
                try {
                    var myTop = this.getAbsolutePosition().T;
                    var expand = false;
                    var children = this.getChildren();
                    for(var i=0; i<children.length; i++) {
                        var child = children[i];
                        if(child.getAbsolutePosition().T > myTop) {
                            expand = true;
                            break;
                        }
                    }
                    var height = expand ? 40 : 20;
                    var dimArr = this.getParent().getDimensionArray();
                    var pHeight = dimArr[0];
                    if(height != pHeight) {
                        var parent = this.getParent();
                        dimArr[0] = height;
                        parent.setDimensionArray(dimArr, true);
                    }
                }
                catch(ex) {
                    ux.admin.Main.logException(ex);
                }
            }

            TabPane_prototype.onRemoveChild = function() {
                this.jsxsuper();
                this.testHeight();
            };

            TabPane_prototype.repaint = function() {
                this.jsxsuper();
                var testPane = this;
                setTimeout(function() {
                testPane.testHeight();
                }, 10);
}

            TabPane_prototype.removeChild = function(vntItem) {
                var child = vntItem;
                if(!(child instanceof jsx3.app.Model)) {
                    child = this.getChild(child);
                }
                var parent = this.getParent();
                if(parent) {
                    parent = parent.getParent();
                    if(parent) {
                                if(parent.selectedTab == child) {
                                    parent.selectedTab = null;
                                }
                                parent.getContentPane().removeChild(child.getContentPane());
                    }
                }
                var ret = this.jsxsuper(vntItem);
                var testPane = this;
                setTimeout(function() {
                testPane.testHeight();
                }, 10);
                this.repaint();
                return ret;
}

            TabPane_prototype.removeChildren = function(arrChildren) {
                var parent = this.getParent();
                if(parent) {
                    parent = parent.getParent();
                    if(parent) {
                                for(var p = 0; p < arrChildren.length; p++) {
                                    var child = arrChildren[p];
                                    if(!(child instanceof jsx3.app.Model)) {
                                        child = this.getChild(child);
                                    }
                                    if(parent.selectedTab == child) {
                                        parent.selectedTab = null;
                                        break;
                                    }
                                    parent.getContentPane().removeChild(child.getContentPane());
                                }
                    }
                }
                var ret = this.jsxsuper(arrChildren);
                var testPane = this;
                setTimeout(function() {
                testPane.testHeight();
                }, 10);
                this.repaint();
                return ret;
}
        });
